package com.wsz.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wsz.pojo.Essays;
import com.wsz.pojo.R;
import com.wsz.pojo.UserInfo;
import com.wsz.pojo.dto.EssayDto;
import com.wsz.pojo.query.EssayQuery;
import com.wsz.user.service.EssaysService;
import com.wsz.user.mapper.EssaysMapper;
import com.wsz.user.service.UserInfoService;
import com.wsz.user.utils.RedisUtils;
import com.wsz.utils.SecurityContextUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author wangs
* @description 针对表【essays】的数据库操作Service实现
* @createDate 2023-01-27 21:18:29
*/
@Service
public class EssaysServiceImpl extends ServiceImpl<EssaysMapper, Essays>
    implements EssaysService{

    @Autowired
    private UserInfoService userInfoService;


    @Autowired
    private RedisUtils redisUtils;

    @Override
    public R release(EssayDto essayDto) {

        String content = essayDto.getContent();
        String description = essayDto.getDescription();
        String title = essayDto.getTitle();

        if(StringUtils.isBlank(content)){
            return R.error().message("内容为空");
        }

        if(StringUtils.isBlank(description)){
           description = "这个人很懒什么也没留下";
        }

        if(StringUtils.isBlank(title)){
            return R.error().message("标题为空");
        }


        if(content.length() > 10000){
            return R.error().message("文章内容不能超过一万字");
        }

        if(description.length() > 200) {
            return R.error().message("介绍内容过长");
        }

        if(title.length() > 30){
            return R.error().message("标题内容不能超过30字");
        }

        Essays essays = new Essays();
        BeanUtils.copyProperties(essayDto,essays);

        String userName = SecurityContextUtils.getUserUserName();

        essays.setAuthor(userName);

        boolean save = this.save(essays);

        if (save) return R.ok();

        return R.error().message("服务器匆忙,请稍后再试");
    }

    @Override
    public R getEssays(String userId,Long limit, Long page, EssayQuery essayQuery) {

        Object o = redisUtils.get("acl_essay_" + userId);
        redisUtils.del("acl_essay_" + userId);
        UserInfo byId = null;

        if(o == null)
        byId = userInfoService.getById(userId);

        if(o == null && byId == null) return R.error().message("不存在该用户");

        String description = essayQuery.getDescription();
        String title = essayQuery.getTitle();
        Boolean sorted = essayQuery.getSorted();
        Boolean supported = essayQuery.getSupported();

        QueryWrapper<Essays> queryWrapper = new QueryWrapper<>();

        if(StringUtils.isNotBlank(title)){
            queryWrapper.like("title",title);
        }

        if(StringUtils.isNotBlank(description)){
            queryWrapper.like("description",description);
        }

        if(sorted) queryWrapper.orderByDesc("create_time");


        if(supported) queryWrapper.orderByDesc("support");

        if(byId != null) {
            queryWrapper.eq("author",byId.getUsername());
        }

        queryWrapper.select("id,title,description","author","support");


        Page<Essays> pages =  new Page<>(page,limit);

        this.baseMapper.selectPage(pages,queryWrapper);

        List<Essays> records = pages.getRecords();

        long total = pages.getTotal();

        return R.ok().data("records",records).data("total",total);
    }

    @Override
    public R updateEssay(EssayDto essayDto) {
        String content = essayDto.getContent();
        String description = essayDto.getDescription();
        String title = essayDto.getTitle();

        if(StringUtils.isBlank(content)){
            return R.error().message("内容为空");
        }

        if(StringUtils.isBlank(description)){
            description = "这个人很懒什么也没留下";
            essayDto.setDescription(description);
        }

        if(StringUtils.isBlank(title)){
            return R.error().message("标题为空");
        }


        if(content.length() > 10000){
            return R.error().message("文章内容不能超过一万字");
        }

        if(description.length() > 200) {
            return R.error().message("介绍内容过长");
        }

        if(title.length() > 30){
            return R.error().message("标题内容不能超过30字");
        }

        Essays essays = new Essays();
        BeanUtils.copyProperties(essayDto,essays);


        boolean b = this.updateById(essays);
        if(b) return R.ok();
        return R.error().message("修改失败");
    }

    @Override
    public Integer getEssaysCount(String date) {
        return this.baseMapper.getEssaysCount(date);
    }
}




